//
//  ViewController.m
//  CustomView
//
//  Created by hoho on 14-11-13.
//  Copyright (c) 2014年 thinkcart.net. All rights reserved.
//

#import "ViewController.h"
#import "PSTAlertController.h"

@interface ViewController ()

@end

@implementation ViewController {
    CMPopTipView *myPopTipView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)checkLogin:(id)sender {
    
    NSString * regex = @"^[\u4e00-\u9fa5A-Za-z0-9_]+$";
    NSPredicate * pred  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:_userNameInput.text];
    
    if(!isMatch){
        [self showPopTipView:@"请输入中文、数字，字母，下划线的组合" :_userNameInput];
        return;
    }
    regex = @"^\\S{6,16}$";
    pred  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    isMatch = [pred evaluateWithObject:_userPwdInput.text];
    if(!isMatch){
        [self showPopTipView:@"请输入6至16位非空白字符" :_userPwdInput];
        return;
    }
    
    PSTAlertController *alertController = [PSTAlertController alertWithTitle:@"登录成功！" message:@"别紧张，测试一下而已！"];
    [alertController addAction:[PSTAlertAction actionWithTitle:@"确定" handler:nil]];
    [alertController showWithSender:nil controller:self animated:YES completion:NULL];
}

- (void)showPopTipView :(NSString *)message :(id)view{
    if(myPopTipView == nil){
        myPopTipView = [[CMPopTipView alloc] initWithMessage:message];
        myPopTipView.delegate = self;
        myPopTipView.backgroundColor = [UIColor redColor];
        myPopTipView.textColor = [UIColor yellowColor];
        myPopTipView.dismissTapAnywhere = YES;
        [myPopTipView autoDismissAnimated:YES atTimeInterval:5.0];
    }
    [myPopTipView presentPointingAtView:view inView:self.view animated:YES];
}

- (void)dismissPopTipView {
    [myPopTipView dismissAnimated:NO];
    myPopTipView = nil;
}


#pragma mark CMPopTipViewDelegate methods
- (void)popTipViewWasDismissedByUser:(CMPopTipView *)popTipView {
    // User can tap CMPopTipView to dismiss it
    myPopTipView = nil;
}

@end
